var webpath=$("#getpath").attr('href');
var Intention=$("#Intention").html();
var Talk=$("#Talk").html();
var Registr=$("#Registr").html();
var Declare=$("#Declare").html();
var NRegistr=$("#NRegistr").html();
var WRegistr=$("#WRegistr").html();
function selectuser(id,txt)
{
	$("#response_select_box").css({"visibility":"hidden","display":"none"});
	$("#ProjectSecondOwner").val(id);
	$("#second_owner").html(txt);
	if(document.getElementById("add_drop_float_click")==null)
	{
		var pid=$("#ProjectId").val();
		$.ajax({     	   
	        url: webpath+'projects/editsecondowner/'+pid+'/'+id, 
	        success:function(data){
				showmessage("项目第二负责人设置成功");
	        }      
	     });
	}
}
function clearowner()
{
	$("#ProjectSecondOwner").val("");
	$("#second_owner").html("");
	$("#response_select_box").css({"visibility":"hidden","display":"none"});
	if(document.getElementById("add_drop_float_click")==null)
	{
		var pid=$("#ProjectId").val();
		$.ajax({     	   
	        url: webpath+'projects/editsecondowner/'+pid, 
	        success:function(data){
				showmessage("项目第二负责人暂无");
	        }      
	     });
	}
}
function selectregdecl(obj)
{
	var phase=$(obj).find("option:selected").text();
	var reg_type=$("#reg_type").get(0).selectedIndex;
	if(phase==Registr){
		showselect("#reg_type");
		hideselect("#nreg_phase");
		hideselect("#wreg_phase");
		hideselect("#decl_phase");
	}else if(phase==Declare){
		showselect("#decl_phase");
		hideselect("#reg_type");
		hideselect("#nreg_phase");
		hideselect("#wreg_phase");
	}else{
		hideselect("#reg_type");
		hideselect("#nreg_phase");
		hideselect("#wreg_phase");
		hideselect("#decl_phase");
	}
	$("#ProjectTxtphase").val(phase);
	if(reg_type!="0" && $("#reg_type").attr("class")==""){
		selectregtypedecl("#reg_type");
	}
}
function selectregtypedecl(obj)
{
	var reg_type=$(obj).find("option:selected").text();
	if(reg_type==NRegistr){
		showselect("#nreg_phase");
		hideselect("#wreg_phase");
	}else if(reg_type==WRegistr){
		showselect("#wreg_phase");
		hideselect("#nreg_phase");
	}else{
		hideselect("#nreg_phase");
		hideselect("#wreg_phase");
	}
	$("#ProjectTxtregType").val(reg_type);
}
function showselect(objid)
{
	$(objid).attr("class","");
	$(objid).parent(".selector").removeClass("txthide");
}
function hideselect(objid)
{
	$(objid).attr("class","txthide");
	$(objid).parent(".selector").addClass("txthide");
}
function settxt(myobj,objvalue,strname,strid,liststr,btnadd){
	
	var isselected=$(myobj).attr("checked");
	var addvalue=myobj.value;
	var addcontent=objvalue;

	var ydata=$(strname).val();
	var yvalue=$(strid).val();
	if(!isselected){
		if(ydata.indexOf(addcontent+",")>=0){
			yvalue=yvalue.replace(addvalue+",","");
			$(strid).val(yvalue);

			ydata=ydata.replace(addcontent+",","");
			$(strname).val(ydata);
		}
	}
	else{
		var data=ydata+addcontent+",";
		var value=yvalue+addvalue+",";
		if(ydata=="")
		{
			data=addcontent+",";
			value=addvalue+",";
		}
		$(strname).val(data);
		$(strid).val(value);
	}
	settvalue(strname,strid,liststr,btnadd);
}
function settvalue(strname,strid,liststr,btnadd)
{
	var data=$(strname).val();
	var value=$(strid).val();
	if(data){
		var objcvid=data.split(",");
		var objcvalue=value.split(",");
		var cvalue="";
		var dan="'";
		var clname="0";
		if(document.getElementById(btnadd)==null)
		{
			clname="1";
		}
		for(i=0;i<objcvid.length;i++)
		{
			if(objcvid[i]!=""){
				var delbtn='<a style="float:left" href="javascript:void(0)" onclick="selecttag('+dan+objcvalue[i]+dan+',this,'+dan+strname+dan+','+dan+strid+dan+')"></a>';
				if(clname=="1"){
					delbtn="";
				}
				cvalue+= '<li class="left"><span><p class="left">'+objcvid[i]+'</p>'+delbtn+'</span></li>';
			}
		}
		$(liststr).html(cvalue);
	}else{
		$(liststr).html("");
	}
}
function selecttag(id,obj,strname,strid)
{
	$("#chbox"+id).attr("checked",false);
	$(":checkbox").uniform();
	$(obj).parent().parent().remove();
	
	var addvalue=id;
	var addcontent=$(obj).parent().text();
	var ydata=$(strname).val();
	var yvalue=$(strid).val();
	yvalue=yvalue.replace(addvalue+",","");
	$(strid).val(yvalue);
	ydata=ydata.replace(addcontent+",","");
	$(strname).val(ydata);
}
function addexamineremark(obj)
{
	var phase=$(obj).find("option:selected").text();
	if(phase.indexOf('前置')>-1)
	{
		$('#examineremark').attr("style","");
	}else{
		$('#examineremark').attr("style","display:none");
	}
}
function showtvalue(valueidlist,strname,strid,tagid,btnadd){
	var cvidlist="";
	if(document.getElementById(valueidlist)!=null){
		cvidlist=$("#"+valueidlist).html();
	}
	if(cvidlist!="")
	{
		var objcvid=cvidlist.split(",");
		var cvalue="";
		for(i=0;i<=objcvid.length;i++)
		{
			if(objcvid[i]!=""){
				if($("#span"+objcvid[i]).html()!=null){
					 cvalue+=$("#span"+objcvid[i]).html()+',';
					 $("#chbox"+objcvid[i]).attr("checked",true);
				}
			}
		}
		$(strname).val(cvalue);
		$(strid).val(cvidlist);
		settvalue(strname,strid,"#"+tagid,btnadd);
	}
}
if($("#ProjectId").val()!=""){
	if(document.getElementById("ProjectPhase")!=null){
		selectregdecl("#ProjectPhase");
	}
	if(document.getElementById("ProjectTxtregType")!=null){
		if($("#ProjectTxtregType").val()!=""){
			selectregtypedecl("#reg_type");
		}
	}
	
	showtvalue("pcvalueidlist","#ProjectIndustryname",
			"#ProjectIndustryid","taglist","add_drop_float_click");
	showtvalue("informationfromidlist","#ProjectInformationfromname",
			"#ProjectInformationfromid","taglistctim","add_drop_float_clickct");
	showtvalue("comptypeidlist","#ProjectCompanytypename",
			"#ProjectCompanytypeid","taglistct","add_drop_float_clickct");
	
	if(document.getElementById("nreg_phase")!=null){
		if($("#nreg_phase").attr("class")==""){
		addexamineremark("#nreg_phase");}
		if($("#wreg_phase").attr("class")==""){
		addexamineremark("#wreg_phase");}
		if($("#decl_phase").attr("class")==""){
		addexamineremark("#decl_phase");}
	}
}
function checksave()
{
	var mail=$("#ProjectEmail").val();
	var projectame=$("#ProjectProjectName").val();
	if($("#projectnamemessage").css('display')=="block"){
		return false;
	}
	if($("#projectcnamemessage").css('display')=="block"){
		return false;
	}
	if(projectame.trim() == ''){
		$("#projectnamemessage").css('display','block');
		return false;
	}else{
		$("#projectnamemessage").css('display','none');
	}
	if($("#projectemailmessage").css('display')=="block")
	{
		return false;
	}
	if($("#ProjectId").val()!=""){
		var phase=$("#ProjectPhase").find("option:selected").text();
		var reg_type=$("#reg_type").get(0).selectedIndex;
		if(phase==Registr && reg_type=="0")
		{
			showmessage('请选择子分类');return false;
		}
	}
	return true;
}
var aid="";
function setphase(id,sub){
	$("#ui-dialog-title-1").html("阶段设置提示");
	$(".confirm_model").dialog( "open" );
	$("#ok").click(function() {
		setphases(id,sub);
		$(".confirm_model").dialog( "close" );
		$('#ok').unbind("click");
	});
}
function setphases(id,sub)
{
	var seltabl=$("#ProjectSelectPhase").val();
	var actions="editsubphase";
	var h=$("#tbl"+id).find(".strphase").html();
	if(h.indexOf('前置')>-1)
	{
		$('#examineremark').attr("style","");
	}else{
		$('#examineremark').attr("style","display:none");
	}
	if(h==Intention || h==Talk || h==Registr || h==Declare)
	{
		seltabl=$("#ProjectSelectphase").val();
		actions="editphase";
	}
	if(h==NRegistr || h==WRegistr)
	{
		seltabl=$("#ProjectSelectphase").val();
		actions="editregtype";
	}
	if(aid){
		$("#tbl"+aid).attr("class","");
		if(sub=="1"){
			$("#tbl"+aid).attr("class","subphase");
		}
		$("#tbl"+id).attr("class","selected");
	}
	else{
		$("#tbl"+seltabl).attr("class","");
		if(sub=="1"){
			$("#tbl"+seltabl).attr("class","subphase");
		}
		$("#tbl"+id).attr("class","selected");
	}
	var pid=$("#ProjectId").val();
	var urlpath=webpath+'projects/'+actions+'/'+pid+'/'+id;
	$.ajax({     	   
        url: urlpath, 
        success:function(data){
			if(h.indexOf('前置')>-1)
			{
				showmessage("阶段保存成功,请填写前置审批备注。");
			}else{
			showmessage("阶段保存成功");}
        	if(h==Registr || h==NRegistr || h==WRegistr || h==Declare )
			{
				urlpath=webpath+'projects/showinfo/'+pid+'/'+parseInt(10*Math.random());
				getphase(urlpath);
				aid="";
				//location.href=urlpath;
			}
        }      
     });
	aid=id;
}
function getphase(urlpath)
{
	$.ajax({   
        url: urlpath, 
        success:function(data){
			var phasehtml=$(data).find(".form_right_stage").html();
			$(".form_right_stage").html(phasehtml);
		}
	});
}